package qeug.shsh.opplugin;

import com.alibaba.nacos.common.utils.StringUtils;
import com.hankcs.hanlp.collection.sequence.SString;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.ClientProperties;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.DateRangeEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;

import javax.naming.ldap.Control;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @projectName: qeug-cosmic-root-project
 * @package: qeug.shsh.opplugin
 * @className: tripentryBillPlugin
 * @author: wenyx
 * @description: 行程信息"成本中心过滤"/"行程期间"
 * @date: 2023/11/28 15:12
 * @version: 1.0
 */
public class TripentryBillPlugin extends AbstractFormPlugin implements BeforeF7SelectListener {
    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        ListShowParameter formShowParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
        List<QFilter> qFilter = new ArrayList<>();
        DynamicObject costdept = (DynamicObject) this.getModel().getValue("entrycostdept");
        qFilter.add(new QFilter("qeug_adminorg.number", QCP.equals, costdept.get("number")));
//        qFilter.add(new QFilter("isleaf", QCP.equals,true));
        formShowParameter.getListFilterParameter().setQFilters(qFilter);
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit control = this.getView().getControl("std_entrycostcenter");
        control.addBeforeF7SelectListener(this);
        this.addClickListeners("startdate");
        this.addClickListeners("enddate");
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String fieldKey = e.getProperty().getName();
        if ("entrycostdept".equals(fieldKey)) {
            DynamicObject costdept = (DynamicObject) this.getModel().getValue("entrycostdept");

            QFilter[] qFilters = new QFilter[]{
                    new QFilter("number", QCP.equals, costdept.get("number"))
            };
            DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bos_costcenter", "id", qFilters);
            this.getModel().setValue("std_entrycostcenter", dynamicObject);
        } else if ("enddate".equals(fieldKey)) {
            ChangeData[] changeSet = e.getChangeSet();
            ChangeData changeData = changeSet[0];
            Object newValue = changeData.getNewValue();//新值
            Date date = (Date) newValue;
            LocalDateTime currentDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
            currentDateTime = currentDateTime.withMinute(0).withSecond(0).withNano(0);
            DateRangeEdit headFieldEdit = this.getView().getControl("tirpdate");//行程期间
            String key_headdateend = headFieldEdit.getEndDateFieldKey();//结束日期
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String currentDateTimeString = currentDateTime.format(formatter);
            this.getModel().setValue(key_headdateend, currentDateTimeString);
        }
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        DynamicObject costdept = (DynamicObject) this.getModel().getValue("entrycostdept");//费用承担部门
        Object qeug_std_entrycostcenter = this.getModel().getValue("std_entrycostcenter");//成本中心
        if (qeug_std_entrycostcenter == null) {
            if (costdept != null) {
                QFilter[] qFilters = new QFilter[]{
                        new QFilter("number", QCP.equals, costdept.get("number"))
                };
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bos_costcenter", "id", qFilters);
                this.getModel().setValue("std_entrycostcenter", dynamicObject);
            }
        }
/*        DynamicObject tripdate = (DynamicObject) this.getModel().getValue("from");//出发地
        if (tripdate != null) {
            DateRangeEdit headFieldEdit = this.getView().getControl("tirpdate");//行程期间
            String key_headdateend = headFieldEdit.getEndDateFieldKey();//结束日期
            this.getView().setEnable(false, key_headdateend);
            this.getView().setEnable(false, "entrycostdept");
        }*/
//        Object triparea = this.getModel().getValue("triparea");//出差地域
//        if (triparea == null) {
//            DateRangeEdit headFieldEdit = this.getView().getControl("tirpdate");//行程期间
//            LocalDate currentDate = LocalDate.now();
//            LocalDateTime currentDateTime = LocalDateTime.of(currentDate, LocalTime.MIDNIGHT);
//            LocalDateTime nextDayDateTime = currentDateTime.plusDays(1);
//
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//            String currentDateTimeString = currentDateTime.format(formatter);
//            String nextDayDateTimeString = nextDayDateTime.format(formatter);
//
//            String key_headdatestart = headFieldEdit.getStartDateFieldKey();//开始日期
//            String key_headdateend = headFieldEdit.getEndDateFieldKey();//结束日期
//            this.getModel().setValue(key_headdatestart, currentDateTimeString);
//            this.getModel().setValue(key_headdateend, nextDayDateTimeString);
//        }
    }

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        HashMap<String, Object> fieldMap = new HashMap<>();
        HashMap<String, Object> itemMap = new HashMap<>();
        //设置文本是否可编辑的属性
        itemMap.put(ClientProperties.Editable, false);
        //把属性map添加到控件的map中，在元数据中查看过后key为item
        fieldMap.put("item", itemMap);
        DateRangeEdit headFieldEdit = this.getView().getControl("tirpdate");//行程期间
        String key_headdateend = headFieldEdit.getEndDateFieldKey();//结束日期
        //同步指定元数据到文本控件
//        this.getView().updateControlMetadata(key_headdateend, fieldMap);
    }
}
